<?php
    
    class User extends CI_Controller {
        
        public function __construct()
        {
            parent::__construct();
            date_default_timezone_set("PRC");
   
            $this->load->model('userinfo_model');
            $this->load->helper('url_helper');
        }
        
        public function logout()
        {
            session_destroy();
            
            redirect('user/login');
        }
        
        public function login()
        {
            //echo "login:"._SESSION;
            if (isset($_SESSION['userId']))
            {
                $this->load->view('login');
                 return;
            }
            
            $this->load->helper('form');
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('username', '用户名', 'required' );
            $this->form_validation->set_rules('password', '密码', 'required' );
            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('login');
            }
            else
            {
                $islogin = $this->userinfo_model->login();
                if ($islogin) {
//                    $level =  $_SESSION['level'];
                    redirect('user/index');
                }
                else{
                    $this->load->view('login');
                }
            }
            
        }
        
        
        
        public function index()
        {
            if (!isset($_SESSION['username']))
            {
                redirect('user/login');  ;
            }
            
            $data['userinfos'] = $this->userinfo_model->getUsers();
            $data['title'] = '用户管理';
            
            $this->load->view('templates/header', $data);
            $this->load->view('user', $data);
            $this->load->view('templates/footer');
        }
        
        public function create()
        {
            if (!isset($_SESSION['username']))
            {
                redirect('user/login');
            }
            
            $this->load->helper('form');
            $this->load->library('form_validation');
            
            $data['title'] = '添加用户';
            
            $this->form_validation->set_rules('name', '用户名', 'required');
            $this->form_validation->set_rules('password', '密码', 'required');
            
            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('useradd');
                $this->load->view('templates/footer');
                
            }
            else
            {
                $this->userinfo_model->add();
                 redirect('user/index');
            }
        }
        
        public function update($userId)
        {
            if (!isset($_SESSION['username']))
            {
                redirect('user/login');
            }
            
            $this->load->helper('form');
            $this->load->library('form_validation');
            
            $data['userInfo'] = $this->userinfo_model->getOneUser($userId);
            
           // $this->form_validation->set_rules('name', '用户名', 'required');
            $this->form_validation->set_rules('password', '密码', 'required');
            //echo "this.".$userId;
            
            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('userupdate');
                $this->load->view('templates/footer');
                
            }
            else
            {
                //echo "this.".$userId;
                $this->userinfo_model->update();
                redirect('user/index');
            }
        }

        
        public function delete($userId)
        {
            if (!isset($_SESSION['username'])) {  
                redirect('user/login');  ;  
            }  
            
            $data['title'] = '删除用户';
            
            if ($userId > 1) {
                $this->userinfo_model->delete($userId);
            }
       
            redirect('user/index');
    
        }
        
    }
